package com.ruoyi.project.men.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.men.domain.MenProtocolClassDO;
import com.ruoyi.project.men.domain.MenProtocolInfoDO;
import com.ruoyi.project.men.service.MenProtocolClassService;
import com.ruoyi.project.men.service.MenProtocolInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务协议/文档中心详情Controller
 *
 * @author long
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/men/menProtocolInfo")
public class MenProtocolInfoController extends BaseController
{
	@Autowired
	private MenProtocolInfoService menProtocolInfoService;
	@Autowired
	private MenProtocolClassService menProtocolClassService;

	/**
	 * 查询服务协议/文档中心详情列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menProtocolInfo:list')")
	@GetMapping("/list")
	public TableDataInfo list(MenProtocolInfoDO menProtocolInfo)
	{
		startPage();
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menProtocolInfo);
		stringObjectMap.put("delFlag","0");
		stringObjectMap.put("sort","ca_at");
		stringObjectMap.put("order","desc");
		List<MenProtocolInfoDO> list = menProtocolInfoService.likeList(stringObjectMap);
		if(list.size()>0){
			for (MenProtocolInfoDO menProtocolInfoDO : list) {
				MenProtocolClassDO menProtocolClassDO = menProtocolClassService.get(menProtocolInfoDO.getClassId());
				if(menProtocolClassDO!=null){
					menProtocolInfoDO.setClassId(menProtocolClassDO.getName());
				}else{
					menProtocolInfoDO.setClassId("");
				}

			}
		}
		return getDataTable(list);
	}

	/**
	 * 导出服务协议/文档中心详情列表
	 */
	@PreAuthorize("@ss.hasPermi('men:menProtocolInfo:export')")
	@Log(title = "服务协议/文档中心详情", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(MenProtocolInfoDO menProtocolInfo)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menProtocolInfo);
		List<MenProtocolInfoDO> list = menProtocolInfoService.list(stringObjectMap);
		ExcelUtil<MenProtocolInfoDO> util = new ExcelUtil<MenProtocolInfoDO>(MenProtocolInfoDO.class);
		return util.exportExcel(list, "menProtocolInfo");
	}

	/**
	 * 获取服务协议/文档中心详情详细信息
	 */
	@PreAuthorize("@ss.hasPermi('men:menProtocolInfo:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") String id)
	{
		AjaxResult ajax = AjaxResult.success();
		Map<String,Object> map = new HashMap<>();
		map.put("sort","ca_at");
		map.put("order","desc");
		map.put("delFlag","0");
		ajax.put("classList",menProtocolClassService.list(map));
		ajax.put("data",menProtocolInfoService.get(id));
		return ajax;
	}

	/**
	 * 新增服务协议/文档中心详情
	 */
	@PreAuthorize("@ss.hasPermi('men:menProtocolInfo:add')")
	@Log(title = "服务协议/文档中心详情", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MenProtocolInfoDO menProtocolInfo)
	{
		menProtocolInfo.setId(IdUtil.simpleUUID());
		menProtocolInfo.setDelFlag(0);
		menProtocolInfo.setCaAt(new Date());
		return toAjax(menProtocolInfoService.save(menProtocolInfo));
	}

	/**
	 * 修改服务协议/文档中心详情
	 */
	@PreAuthorize("@ss.hasPermi('men:menProtocolInfo:edit')")
	@Log(title = "服务协议/文档中心详情", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MenProtocolInfoDO menProtocolInfo)
	{
		menProtocolInfo.setUpAt(new Date());
		return toAjax(menProtocolInfoService.update(menProtocolInfo));
	}

	/**
	 * 删除服务协议/文档中心详情
	 */
	@PreAuthorize("@ss.hasPermi('men:menProtocolInfo:remove')")
	@Log(title = "服务协议/文档中心详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				MenProtocolInfoDO menProtocolInfo = menProtocolInfoService.get(string);
				menProtocolInfo.setDelFlag(1);
				menProtocolInfo.setUpAt(new Date());
				menProtocolInfoService.update(menProtocolInfo);
			}
		}
		return toAjax(ids.length);
		//return toAjax(menProtocolInfoService.deleteMenProtocolInfoByIds(ids));
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("men:menProtocolInfo:edit")
	String edit(@PathVariable("id") String id, Model model){
		MenProtocolInfoDO menProtocolInfo = menProtocolInfoService.get(id);
		model.addAttribute("menProtocolInfo", menProtocolInfo);
		List<Map<String,Object>> list = menProtocolClassService.queryAllForIdAndName(new HashMap<String,Object>());
		model.addAttribute("classList", list);
		return "men/menProtocolInfo/edit";
	}
}
